﻿using System;
using System.Text.RegularExpressions;

namespace DDDSample.Domain.Location
{
    public class UnLocode
    {
        private static readonly Regex CodePattern = new Regex("[a-zA-Z]{2}[a-zA-Z2-9]{3}", RegexOptions.Compiled | RegexOptions.CultureInvariant);
        private readonly string _code;

        protected UnLocode()
        {
            
        }

        public UnLocode(string code)
        {
            if (code == null)
            {
                throw new ArgumentNullException("code");
            }
            if (!CodePattern.Match(code).Success)
            {
                throw new ArgumentException(string.Format("Provided code does not comply with a UnLocode pattern ({0})", CodePattern), "code");
            }
            _code = code;
        }

        public virtual string CodeString
        {
            get { return _code; }
        }

        public override string ToString()
        {
            return _code;
        }
    }
}